package controller

import (
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"

	"github.com/gin-gonic/gin"
)

type Base struct{}

func (b *Base) Login(c *gin.Context) {

	var account *request.Login

	c.ShouldBindJSON(&account)

	if account.LoginType == 0 {
		if token, err := services.BaseService.Login(account); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithDetailed(gin.H{
				"access_token": token,
			}, "登录成功!", c)
		}
	} else if account.LoginType == 1 {
		if token, err := services.BaseService.PhoneLogin(account); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithDetailed(gin.H{
				"access_token": token,
			}, "登录成功!", c)
		}
	}
}

func (b *Base) LogOut(c *gin.Context) {
	utils.OkWithMessage("登出成功", c)
}

func (b *Base) SendCode(c *gin.Context) {

	var phone *request.SendCode

	c.ShouldBindJSON(&phone)

	err := services.BaseService.SendCode(phone.Phone, "lOGIN_")

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (b *Base) ValidCode(c *gin.Context) {

	var valid *request.ValidCode

	c.ShouldBindJSON(&valid)

	err := services.BaseService.ValidCode("LOGIN_", valid.Phone, valid.Code)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

var BaseController = new(Base)
